package com.mmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.zxing.common.reedsolomon.ReedSolomonDecoder;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.dao.ProductMapper;
import com.mmall.pojo.Category;
import com.mmall.pojo.Product;
import com.mmall.service.IProductService;
import com.mmall.util.DateTimeUtil;
import com.mmall.util.PropertyUtil;
import com.mmall.vo.ProductVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by root on 18-4-7.
 */
@Service
public class ProductServiceImpl implements IProductService{

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    public ServerResponse saveProduct(Product product){
        if (product == null) {
            return ServerResponse.createByError(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        int rs = 0;
        if (product.getId() == null) {
            rs = productMapper.insert(product);
        }else{
            rs = productMapper.updateByPrimaryKeySelective(product);
        }

        if (rs == 0) {
            return ServerResponse.createByError("save product failed, please try again!");
        }
        return ServerResponse.createBySuccess();
    }


    public ServerResponse setSaleStatus(Integer productId, String status){
        if (productId == null || StringUtils.isBlank(status)) {
            return ServerResponse.createByError(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        int rs = productMapper.updateSaleStatusByPrimaryKey(productId, status);
        if (rs == 0){
            return ServerResponse.createByError("update product status failed, please try again!");
        }
        return ServerResponse.createBySuccess();
    }

    public ServerResponse getProductDetail(Integer productId){
        if (productId == null) {
            return ServerResponse.createByError(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        Product product = productMapper.selectByPrimaryKey(productId);
        if (product == null){
            return ServerResponse.createByError("no such product");
        }
        ProductVO productVO = assmebleProductVO(product);
        return ServerResponse.createBySuccess(productVO);
    }


    private ProductVO assmebleProductVO(Product product){
        ProductVO productVO = new ProductVO();
        productVO.setId(product.getId());
        productVO.setName(product.getName());
        productVO.setStatus(product.getStatus());
        productVO.setCategoryId(product.getCategoryId());
        Category category = categoryMapper.selectByPrimaryKey(product.getCategoryId());
        productVO.setParentCategoryId(category == null ? null : category.getParentId());
        productVO.setImageHost(PropertyUtil.getFtpHost());
        productVO.setCreateTime(DateTimeUtil.dateToStr(product.getCreateTime()));
        productVO.setUpdateTime(DateTimeUtil.dateToStr(product.getUpdateTime()));
        productVO.setDetail(product.getDetail());
        productVO.setMainImage(product.getMainImage());
        productVO.setPrice(product.getPrice());
        productVO.setStock(product.getStock());

        return productVO;
    }

    public ServerResponse  getManageProductList(Integer pageNum, Integer pageSize){
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<Product> productList = productMapper.getProductList();
        PageInfo pageInfo = new PageInfo(productList);
        pageInfo.setList(productList);
        return ServerResponse.createBySuccess(pageInfo);
    }


    public ServerResponse searchProductList(String productName, Integer productId, Integer pageNum, Integer pageSize){
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        if (!StringUtils.isBlank(productName)){
            productName = "%" + productName + "%";
        }
        List<Product> productList = productMapper.searchProductList(productName, productId);
        PageInfo pageInfo = new PageInfo(productList);
        pageInfo.setList(productList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    public ServerResponse searchProductList(Integer categoryId, String keyword, Integer pageNum, Integer pageSize, String orderBy) {
        if (categoryId == null && StringUtils.isBlank(keyword)) {
            PageInfo pageInfo = new PageInfo();
            pageInfo.setList(Collections.emptyList());
            return ServerResponse.createBySuccess(pageInfo);
        }
        if (categoryId != null) {
            Category category = categoryMapper.selectByPrimaryKey(categoryId);
            if (category == null && StringUtils.isBlank(keyword)){
                PageInfo pageInfo = new PageInfo();
                pageInfo.setList(Collections.emptyList());
                return ServerResponse.createBySuccess(pageInfo);
            }
        }

        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        if (Const.PRODUCT_PRICE_DESC_ASC.contains(orderBy)){
            String[] ordebyArray = orderBy.split("_");
            PageHelper.orderBy(ordebyArray[0] + " " + ordebyArray[1]);
        }
        keyword = StringUtils.isBlank(keyword) ? null : "%" + keyword.trim() + "%";
        List<Product> products = productMapper.searchProdsByCateOrName(keyword, categoryId);
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(products);
        return ServerResponse.createBySuccess(pageInfo);
    }


}
